package com.jz.netthings.engine.task;

import com.jz.netthings.appengine.mapper.JzMachineMapper;
import com.jz.netthings.appengine.mapper.LocationMapper;
import com.jz.netthings.appengine.queue.ChairMgr;
import com.jz.netthings.appengine.queue.MachineMgr;
import com.jz.netthings.appengine.queue.ProviderQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时操作缓存的任务
 */
@Component
public class CacheTaskJob {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    JzMachineMapper jzMachineMapper;
    @Autowired
    LocationMapper locationMapper;
    @Autowired
    ProviderQueue providerQueue;

    @Autowired
    RedisTemplate<Object , Object> objectRedisTemplate;

    /**
     * 处理采浆机的缓存
     * 每天晚上 23:05 执行
     */
    @Scheduled(cron = "0 5 23 * * ?")
    public void machineJob(){
        logger.info("重新初始化单采机缓存任务开始....");
        synchronized (CacheTaskJob.class){
            MachineMgr.getInstance().reInitMachine(jzMachineMapper,objectRedisTemplate);
            ChairMgr.getInstance().initChair(locationMapper);
        }
        logger.info("重新初始化单采机缓存任务结束....");
    }
    /**
     * 清空浆员当天登记的缓存
     * 每天晚上 23:00 执行
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void providerQueueJob(){
        logger.info("清空浆员当天登记缓存任务开始....");
        synchronized (CacheTaskJob.class){
            providerQueue.cleanAll();
        }
        logger.info("清空浆员当天登记缓存任务结束....");
    }

    /**
     * 2小时一次，操作一下数据库，避免数据库长时间不访问而导致的报错问题
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void temp(){
        logger.info("...........................");
        locationMapper.selectCount(null);
    }




}
